﻿@{
    ViewBag.Title = "OutGetInfo";
}

@section scripts
{
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js")" type="text/javascript"></script> 
    <link href="~/Scripts/miniui/themes/default/Large-mode.css" rel="stylesheet" type="text/css" />      
    <style>
        body {
            font-size:11pt !important;
        }
    </style>
}

<form id="form1" method="post">
        <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
            <table style="width:100%;">
                <tr>
                    <td style="width:100%;">
                        <a class="mini-button" name="SaveInfo" iconCls="icon-save" onclick="SaveInfo" plain="true">保存</a>
                        <a class="mini-button" iconCls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                    </td>
                </tr>
            </table>                
        </div>    
        <div name="Base" title="出货收款" style="height:100%;overflow:hidden;margin-top:15px;margin-left:40px">
            <table>
                <tr>
                    <td width="100px" height ="40px" align="right">
                        收款项目：</td>
                    <td width="200px" height ="40px">
                        @Html.Partial("~/Views/Modular/LookupPayItemOut.cshtml")
                    </td>
                    <td width="120px" height ="40px" align="right"></td>
                    <td width="160px" height ="40px"></td>
                </tr>
                <tr>
                    <td width="80px" height ="40px" align="right">
                        单据编号：</td>
                    <td width="200px" height ="40px">
                        <input id="FBillNo" name="FBillNo" class="mini-textbox" value="" style="width:200px;" tabindex="1" readonly="true" />
                    </td>
                    <td width="120px" height ="40px" align="right">
                        开单日期：</td>
                    <td width="160px" height ="40px">
                        <input class="mini-datepicker" name="FDate1" format="yyyy-MM-dd" style="width:160px;" value = "" tabindex="5"/> 
                    </td>
                </tr>
                <tr>
                    <td width="80px" height ="40px" align="right">
                        客户名称：</td>
                    <td width="200px" height ="40px">
                        <table>
                            <tr>
                                <td width="70px">
                                    <input id="FCustItemID"  name="FCustItemID" class="mini-buttonedit" style="width:70px;"
                                        onbuttonclick="getCustItem('')" onvaluechanged ="getCustValueChanged" selectOnFocus="true" />
                                </td>
                                <td width="122px">
                                    <input id="FCustName" name="FCustName" class="mini-textbox" style="width:122px;" background-color: #CCCCCC;" 
                                        enabled = "false"  align="left" readonly="readonly"/>
                                </td>
                            </tr>
                        </table>
                    </td>
                    <td width="120px" height ="40px" align="right">
                        当前应收：</td>
                    <td width="160px" height ="40px">
                        <input id="FAmount" name="FAmount" class="mini-spinner" minValue="0" maxValue="99999999" changeOnMousewheel ="false" 
                             style="width:160px;" TabIndex="6" showbutton="false"  format="#,0.00" value="0" readonly="true"/>
                    </td>
                </tr>
                <tr>
                    <td width="80px" height ="40px" align="right">
                        门店选择：</td>
                    <td width="200px" height ="40px">
                        @Html.Partial("~/Views/Modular/LookupStockInfo.cshtml")
                    </td>
                    <td width="120px" height ="40px" align="right">
                        收款金额：</td>
                    <td width="160px" height ="40px">
                        <input id="FGetAmount" name="FGetAmount" class="mini-spinner" minValue="0" maxValue="99999999" changeOnMousewheel ="false" 
                             style="width:160px;" TabIndex="7" showbutton="false"  format="#,0.00" value="0"/>
                    </td>
                </tr>
                <tr>
                    <td width="80px" height ="40px" align="right">单据摘要：</td>
                    <td width="200px" height="40px">
                        <input id="Explanation" name="Explanation" class="mini-textbox" value="" style="width:200px;" tabindex="4" />
                    </td>
                    <td width="120px" height ="40px" align="right">单据状态：</td>
                    <td width="240px" height ="40px">
                        <input name="txtCheckFlag" class="mini-textbox" readonly="readonly"style="width:160px;"/>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><div id="ck1" name="ck1" tabindex="8"  class="mini-checkbox" style ="margin-right:20px" readOnly="false" value ="true" text="连续新增 "></div></td>
                    <td></td>
                    <td></td>
                </tr>
            </table>
        </div>

    <input name="FItemID" class="mini-hidden" />
    <input type="hidden" id="type" value="@ViewBag.type" />

</form>

<script>
    mini.parse();

    function FormShown() {
    }
    function SetInfoData(datas) {
        if (datas.action == "new") {
            NewInfo();
        }
        else {
            $.post("GetOutInfo", { mjson: "[{'FItemID':'" + datas.FItemID + "','FSystemAppID':'" + parent.window.getSystemAppID() + "'}]" }, function (data) {
                var form = new mini.Form("form1");
                var mdata = mini.decode(data);
                form.setData(mdata.data[0]);
                mini.getbyName("FStockID").setText(mdata.data[0].FStockNumber);
                mini.getbyName("FStockID").setValue(mdata.data[0].FStockID);
                mini.get("FStockName").setValue(mdata.data[0].FStockName);
                mini.get("FCustItemID").setValue(mdata.data[0].FCustID);
                mini.get("FCustItemID").setText(mdata.data[0].FCustNumber);
                mini.get("FCustName").setValue(mdata.data[0].FCustName);
                mini.getbyName("FDate1").setValue(mdata.data[0].FDate);
                mini.getbyName("txtCheckFlag").setValue(mdata.data[0].FCheckStatus);
                mini.getbyName("Explanation").setValue(mdata.data[0].FExplanation);
                mini.getbyName("ck1").setChecked(false);
                mini.getbyName("FItemID").setValue(datas.FItemID);
                mini.getbyName("FPayItemNumber").setValue(mdata.data[0].FItemTypeID);
                mini.getbyName("FPayItemNumber").setText(mdata.data[0].FItemNumber);
                mini.getbyName("FPayItemName").setValue(mdata.data[0].FItemName);
                getCustValueChanged();
                if (mdata.data[0].FCheckFlag != 0)
                    BillStatu(false);
            });
        }
    }

    function NewInfo() {
        var nowDate = new Date();
        mini.getbyName("FDate1").setValue(nowDate);
        mini.getbyName("FItemID").setValue(0);
        mini.getbyName("FStockID").setText('');
        mini.getbyName("FStockID").setValue('');
        mini.get("FStockName").setValue('');
        mini.get("FCustItemID").setValue('');
        mini.get("FCustItemID").setText('');
        mini.get("FCustName").setValue('');
        mini.getbyName("FAmount").setValue(0);
        mini.getbyName("FGetAmount").setValue(0);
        mini.getbyName("Explanation").setValue("");
        mini.getbyName("ck1").setChecked(false);
        mini.getbyName("FPayItemNumber").setValue(0);
        mini.getbyName("FPayItemNumber").setText('');
        mini.getbyName("FPayItemName").setValue('');
        setDefault();
        mini.getbyName("FGetAmount").focus();
    }

    function setDefault() {
        $.post("GetDefaultOut", { mjson: "[{'FSystemAppID':'" + parent.window.getSystemAppID() + "'}]" }, function (data) {
            var mdata = mini.decode(data);
            if (mdata.data.length > 0) {
                mini.getbyName("FStockID").setText(mdata.data[0].FStockNumber);
                mini.getbyName("FStockID").setValue(mdata.data[0].FStockID);
                mini.getbyName("FStockName").setValue(mdata.data[0].FStockName);
                mini.getbyName("FCustItemID").setValue(mdata.data[0].FCustID);
                mini.getbyName("FCustItemID").setText(mdata.data[0].FCustNumber);
                mini.getbyName("FCustName").setValue(mdata.data[0].FCustName);
                mini.getbyName("FPayItemNumber").setValue(mdata.data[0].FItemTypeID);
                mini.getbyName("FPayItemNumber").setText(mdata.data[0].FItemNumber);
                mini.getbyName("FPayItemName").setValue(mdata.data[0].FItemName);
                getCustValueChanged();
            }
        });
    }


    function BillStatu(bool) {
        mini.get("FCustItemID").setEnabled(bool);
        mini.get("FStockID").setEnabled(bool);
        mini.getbyName("FGetAmount").setEnabled(bool);
        mini.getbyName("FBillNo").setEnabled(bool);
        mini.get("FCustName").setEnabled(bool);
        mini.get("FStockName").setEnabled(bool);
        mini.getbyName("FAmount").setEnabled(bool);
        mini.getbyName("Explanation").setEnabled(bool);
        mini.getbyName("txtCheckFlag").setEnabled(bool);
        mini.getbyName("SaveInfo").setEnabled(bool);
        mini.getbyName("FDate1").setEnabled(bool);
    }

    function SaveInfo() {
        mini.getbyName("SaveInfo").setEnabled(false);
        var form = new mini.Form("#form1");
        var data = form.getData(true, false);
        if (!validata(data)) {
            mini.getbyName("SaveInfo").setEnabled(true);
            return;
        }
        data.type = $("#type").val();
        var json = mini.encode(data);
        $.post("SaveOutInfo", { mjson: json, mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.alert(ret.msg, '提示', function () { CloseWindow(); });
            }
            else {
                mini.alert("异常:" + ret.error);
            }
            mini.getbyName("SaveInfo").setEnabled(true);
        });
    }

    function validata(data) {
        if (data.FDate == "") {
            mini.alert("日期未设置");
            return false;
        }
        if (data.FStockID == "") {
            mini.alert("门店未选择");
            return false;
        }
        if (data.FCustItemID == "") {
            mini.alert("客户未选择");
            return false;
        }
        if (data.FGetAmount == "0") {
            mini.alert("收款金额需要大于零");
            return false;
        }
        return true;
    }

    function getCustItem(mSearchKey) {
        mini.open({
            url: "../GetItem/Index",
            title: "客户选择", width: 730, height: 460,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "new", sTbName: "CustInfo", sSearchKey: mSearchKey };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                if (action.length > 0) {
                    mini.get("FCustItemID").setValue(action[0].FItemID);
                    mini.get("FCustItemID").setText(action[0].FNumber);
                    mini.get("FCustName").setValue(action[0].FName);
                    mini.get("FAmount").setValue(action[0].FBalanceAmt);
                }
                else {
                    mini.get("FCustItemID").setValue('');
                    mini.get("FCustItemID").setText('');
                    mini.get("FCustName").setValue('');
                    mini.get("FAmount").setValue('0');
                }
            }
        });
    }

    function getCustValueChanged() {
        var sSearchKey = mini.get("FCustItemID").getText();
        var mAppID = parent.window.getSystemAppID();
        if (sSearchKey == "") return;
        var sTableCode = "CustInfo";
        var json = "[{";
        json = json + "'FSystemAppID':" + "'" + mAppID + "',";
        json = json + "'FTbName':" + "'" + sTableCode + "',";
        json = json + "'FClassID':" + "'0',";
        json = json + "'FNumber':" + "'" + sSearchKey + "'";
        json = json + "}]";
        $.post("../GetItem/GetItemList", { datajson: json }, function (text) {
            var sdata = mini.decode(text);
            if (sdata.data.length == 1) {
                mini.get("FCustItemID").setValue(sdata.data[0].FItemID);
                mini.get("FCustItemID").setText(sdata.data[0].FNumber);
                mini.get("FCustName").setValue(sdata.data[0].FName);
                mini.get("FAmount").setValue(sdata.data[0].FBalanceAmt);
            }
            else {
                mini.get("FCustItemID").setValue("");
                mini.get("FCustItemID").setText("");
                mini.get("FCustName").setValue("");
                mini.get("FAmount").setValue('0');
            }
        });
    }
</script>